package com.hall.weaponenhancement.service;

import com.hall.TripletProcessResult;
import com.hall.items.aggr.MyWeapon;
import com.hall.items.aggr.MyWeaponLevel;
import com.hall.items.aggr.MyWeaponProperties;
import com.hall.items.repo.MyWeaponRepository;
import com.hall.weaponenhancement.aggr.GoldAccount;
import com.hall.weaponenhancement.aggr.PlayerGoldAccounts;
import dml.accounting.aggr.TransferResult;
import dml.accounting.proc.AccountingProcesses;
import dml.accounting.repo.AccountRepository;
import erp.annotation.Process;
import erp.mongodb.MongodbRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

@Component
public class WeaponEnhancementService {

    @Autowired
    private MyWeaponRepository weaponRepository;

    @Autowired
    private MongodbRepository<PlayerGoldAccounts, Long> playerGoldAccountsRepository;

    private AccountingProcesses accountingProcesses;

    private MongodbRepository<MyWeaponLevel, Long> myWeaponLevelRepository;

    @Autowired
    private MongodbRepository<MyWeaponProperties, Long> myWeaponPropertiesRepository;

    @Autowired
    public WeaponEnhancementService(MongoTemplate mongoTemplate,
                                    AccountRepository<GoldAccount, Long> goldAccountRepository) {
        accountingProcesses = new AccountingProcesses(
                (AccountRepository<?, ?>) goldAccountRepository);
        myWeaponLevelRepository = new MongodbRepository<>(mongoTemplate,
                MyWeaponLevel.class);
    }

    //    @Process(publish = true)
    @Process
    public TripletProcessResult<MyWeaponLevel, MyWeaponProperties, TransferResult<GoldAccount>> enhanceWeapon(
            long playerId, long weaponId) {
        MyWeapon weapon = weaponRepository.find(weaponId);
        if (weapon == null) {
            return null;
        }
        if (!weapon.getPlayerId().equals(playerId)) {
            return null;
        }
        MyWeaponLevel myWeaponLevel = myWeaponLevelRepository
                .take(weaponId);
        if (myWeaponLevel == null) {
            myWeaponLevel = new MyWeaponLevel();
            myWeaponLevel.setId(weaponId);
            myWeaponLevel.setWeapon(weapon);
            MyWeaponLevel existsMyWeaponLevel = myWeaponLevelRepository
                    .putIfAbsent(myWeaponLevel);
            if (existsMyWeaponLevel != null) {
                myWeaponLevel = existsMyWeaponLevel;
            }
        }
        if (myWeaponLevel.getLevel() >= 10) {
            return null;
        }
        myWeaponLevel.add();
        MyWeaponProperties myWeaponProperties = myWeaponPropertiesRepository
                .take(weaponId);
        if (myWeaponProperties == null) {
            myWeaponProperties = new MyWeaponProperties();
            myWeaponProperties.setId(weaponId);
            myWeaponProperties.setWeapon(weapon);
            MyWeaponProperties existsMyWeaponProperties = myWeaponPropertiesRepository
                    .putIfAbsent(myWeaponProperties);
            if (existsMyWeaponProperties != null) {
                myWeaponProperties = existsMyWeaponProperties;
            }
        }
        PlayerGoldAccounts playerGoldAccounts = playerGoldAccountsRepository
                .find(playerId);
        TransferResult<GoldAccount> transferResult = accountingProcesses
                .transfer(playerGoldAccounts.getUserAccountId(),
                        playerGoldAccounts.getSysAccountId(), "10");
        if (transferResult == null) {
            return null;
        }
        myWeaponProperties.addByNewLevel();
        return TripletProcessResult.instance(myWeaponLevel, myWeaponProperties,
                transferResult);
    }

}
